package member.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import member.action.form.MemberActionForm;
import member.service.MemberCRUDService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import document.action.form.DocumentActionForm;
import document.service.DocCRUDService;

public class DAOAction extends DispatchAction {
	private MemberCRUDService service;

	public void setService(MemberCRUDService service){
		this.service = service;
	}
	
	public ActionForward registMember(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MemberActionForm mform = (MemberActionForm)form;
		service.insertMember(form);
		if(form!=null){
			return mapping.findForward("regist_success");
		} else {
			return mapping.findForward("regist_fail");
		}		
	}
	
	public ActionForward deleteMember(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		String id = request.getParameter("id");
		String result = service.deleteMember(id);
		return mapping.findForward(result);
	}
	
	public ActionForward showAllMember(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		List mlist = service.showAllMember();
		request.setAttribute("mlist", mlist);
		return mapping.findForward("list_success");
	}
	
	public ActionForward showMember(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		String id = request.getParameter("id");
		HashMap mmap = service.showMember(id);
		if(mmap!=null){
			request.setAttribute("mmap", mmap);
			return mapping.findForward("show_success");
		} else {
			return mapping.findForward("show_fail");
		}
	}
	
	public ActionForward modifyFormMember(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		String id = request.getParameter("id");
		HashMap mmap = service.showMember(id);
		if(mmap!=null){
			request.setAttribute("mmap", mmap);		
			return mapping.findForward("modify_form_success");
		} else {
			return mapping.findForward("modify_form_fail");
		}
	}
	
	public ActionForward modifyMember(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MemberActionForm mform = (MemberActionForm)form;
		String result = service.modifyMember(form);
		return mapping.findForward(result);
	}
	
	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		String id = request.getParameter("id");
		String password = request.getParameter("password");
		HashMap mmap = service.showMember(id);
		if(mmap.get("id")==null){
			return mapping.findForward("login_fail");
		} else {
			if(password.equals(mmap.get("password"))){
				HttpSession session = request.getSession();
				session.setAttribute("mmap", mmap);
				return mapping.findForward("login_success");
			} else {
				return mapping.findForward("login_fail");
			}
		}
	}
	
	public ActionForward logout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		HttpSession session = request.getSession();
		session.invalidate();
			return mapping.findForward("logout_success");
	}	
	
}
